
<!DOCTYPE html>
<html lang="">


<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
  <meta name="theme-color" content="#202020"/>
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <script src="/js/busuanzi.pure.mini.js" async></script>
  
  
    <meta name="keywords" content="Emberjs,Ember-Teach,Ember-Template," />
  

  
    <meta name="description" content="一个专注一coding的网站。提供丰富编程知识，包括Spring、Java、EmberJS、SpringBoot等等技术。" />
  
  
  <link rel="icon" type="image/x-icon" href="/image/favicon.ico">
  <title>番外篇，路由、模板的执行、渲染顺序 [ Keep Coding ]</title>
  
    <!-- stylesheets list from config.yml -->
    
      <link rel="stylesheet" href="/css/pure-min.css">
    
      <link rel="stylesheet" href="/css/xoxo.css">
    
  
<meta name="generator" content="Hexo 5.0.2"><link rel="alternate" href="/atom.xml" title="Keep Coding" type="application/atom+xml">
</head>

<body>
  <div class="nav-container">
    <nav class="home-menu pure-menu pure-menu-horizontal">
  <a class="pure-menu-heading" href="/">
    <img class="avatar" src="/image/favicon.ico">
    <span class="title">Keep Coding</span>
  </a>

  <ul class="pure-menu-list clearfix">
      
          
            <li class="pure-menu-item"><a href="/" class="pure-menu-link">首页</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/archives" class="pure-menu-link">归档</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/tags" class="pure-menu-link">标签</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/search" class="pure-menu-link">搜索</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/about" class="pure-menu-link">关于</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/atom.xml" class="pure-menu-link">订阅</a></li>
          
      
  </ul>
   
</nav>
  </div>

  <div class="container" id="content-outer">
    <div class="inner" id="content-inner">
      <div class="post-container">
  <article class="post" id="post">
    <header class="post-header text-center">
      <h1 class="title">
        番外篇，路由、模板的执行、渲染顺序
      </h1>
      <span>
        
        <time class="time" datetime="2020-01-18T14:50:37.000Z">
        2020-01-18
      </time>
        
      </span>
      <span class="slash">/</span>
      <span class="post-meta">
      <span class="post-tags">
        <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Ember-Teach/" rel="tag">Ember-Teach</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Ember-Template/" rel="tag">Ember-Template</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Emberjs/" rel="tag">Emberjs</a></li></ul>
      </span>
    </span>
      <span class="slash">/</span>
      <span class="read">
      <span id="busuanzi_value_page_pv"></span> 点击
    </span>
      <span class="slash">/</span>
      <span class="read">阅读耗时 4 分钟</span>
    </header>

    <div class="post-content">
      <p>在Ember中路由和模板的执行都是有一定顺序的，它们的顺序为：主路由-&gt;子路由1-&gt;子路由2-&gt;子路由3-&gt;⋯⋯。模板渲染的顺序与路由执行顺序刚好相反，从最后一个模板开始解析渲染。</p>
<p><strong>注意</strong>：模板的渲染是在所有路由执行完之后，从最后一个模板开始。关于这一点下面的代码会演示验证，官网教程有介绍，点击查看。</p>
<p>比如有一路由格式为<code>application/posts/detail/comments/comment</code>，此时路由执行的顺序为：<code>application/posts</code> -&gt; <code>detail</code> -&gt; <code>comments</code> -&gt; <code>comment</code>，<code>application</code>是项目默认的路由，用户自定义的所有路由都是<code>application</code>的子路由（默认情况下），相对应的模板也是这样，所有用户自定义的模板都是<code>application.hbs</code>的子模板。如果你要修改模板的渲染层次你可以在<code>route</code>中重写<code>renderTemplate</code>回调函数，在函数内使用<code>render</code>方法指定要渲染的模板（如：<code>render(&#39;other&#39;)</code>，渲染到<code>other</code>这个模板上）更多有关信息请查看这里。并且它们对应的文件模板结构如下图：</p>
<p><img src="/content/images/2016/03/28.png" alt="文件模板结构"></p>
<p>路由与模板是相对应的，所以模板的目录结构与路由的目录结构是一致的。<br>你有两种方式构建上述目录：</p>
<ol>
<li>手动创建</li>
<li>使用命令，比如创建<code>comment.js</code>使用命令：<code>ember generate route posts/detail/comments/comment</code>，<a target="_blank" rel="noopener" href="http://ember-cli.com/user-guide/">Ember CLI</a>会自动为我们创建目录和文件。</li>
</ol>
<p>创建好目录结构之后我们添加一些代码到每个文件。运行项目之后你就会一目了然了⋯⋯。<br>下面我按前面讲的路由执行顺序分别列出每个文件的内容。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/routes/posts.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Route.extend(&#123;</span><br><span class="line">    model: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123; </span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;running in posts...&#x27;</span>);</span><br><span class="line">        <span class="keyword">return</span> &#123; <span class="attr">id</span>: <span class="number">1</span>, <span class="attr">routeName</span>: <span class="string">&#x27;The route is posts&#x27;</span>&#125;;</span><br><span class="line">        <span class="comment">// return Ember.$.getJSON(&#x27;https://api.github.com/repos/emberjs/ember.js/pulls&#x27;);</span></span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Route.extend(&#123;</span><br><span class="line"></span><br><span class="line">    model: <span class="function"><span class="keyword">function</span>(<span class="params">params</span>) </span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;params id = &#x27;</span> + params.post_id);</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;running in detail....&#x27;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> &#123; <span class="attr">id</span>: <span class="number">1</span>, <span class="attr">routeName</span>: <span class="string">&#x27;The route is detail..&#x27;</span> &#125;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);    </span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/routes/posts/detail.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Route.extend(&#123;</span><br><span class="line"></span><br><span class="line">    model: <span class="function"><span class="keyword">function</span>(<span class="params">params</span>) </span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;params id = &#x27;</span> + params.post_id);</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;running in detail....&#x27;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> &#123; <span class="attr">id</span>: <span class="number">1</span>, <span class="attr">routeName</span>: <span class="string">&#x27;The route is detail..&#x27;</span> &#125;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);    </span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/routes/posts/detail/comments.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Route.extend(&#123;</span><br><span class="line">    model: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;running in comments...&#x27;</span>);</span><br><span class="line">        <span class="keyword">return</span> &#123; <span class="attr">id</span>: <span class="number">1</span>, <span class="attr">routName</span>: <span class="string">&#x27;The route is comments....&#x27;</span>&#125;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/routes/posts/detail/comments/comment.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Route.extend(&#123;</span><br><span class="line">    model: <span class="function"><span class="keyword">function</span>(<span class="params">params</span>) </span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;params id = &#x27;</span> + params.post_id);</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;running in comment...&#x27;</span>);</span><br><span class="line">        <span class="keyword">return</span> &#123; <span class="attr">id</span>: <span class="number">1</span>, <span class="attr">routeName</span>: <span class="string">&#x27;The route is comment...&#x27;</span>&#125;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>下面是模板各个文件的内容。其列出才顺序与路由的顺序一致。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--  //  app/templates/posts.hbs  --&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- 所有的子路由对应的模板都会渲染到outlet上 --&gt;</span></span><br><span class="line">&#123;&#123;model.routeName&#125;&#125; &gt;&gt; &#123;&#123;outlet&#125;&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- // app/templates/posts/detail.hbs --&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- 所有的子路由对应的模板都会渲染到outlet上 --&gt;</span></span><br><span class="line">&#123;&#123;model.routeName&#125;&#125; &gt;&gt; &#123;&#123;outlet&#125;&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- // app/templates/posts/detail/comments.hbs --&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- 所有的子路由对应的模板都会渲染到outlet上 --&gt;</span></span><br><span class="line">&#123;&#123;model.routeName&#125;&#125; &gt;&gt; &#123;&#123;outlet&#125;&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- // app/templates/posts/detail/comments/comment.hbs --&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- 所有的子路由对应的模板都会渲染到outlet上 --&gt;</span></span><br><span class="line">&#123;&#123;model.routeName&#125;&#125; &gt;&gt; &#123;&#123;outlet&#125;&#125;</span><br></pre></td></tr></table></figure>

<p>下图是路由执行的顺序，并且在执行的过程中渲染路由对应的模板。</p>
<p><img src="/content/images/2016/03/29.png" alt="路由执行的顺序"></p>
<p>从上图中可用清楚的看到当你运行一个URL时，与URL相关的路由是怎么执行的。</p>
<ol>
<li>执行主路由（默认是<code>application</code>），此时进入到路由的<code>model</code>回调方法，并且返回了一个对象<code>&#123; id: 1, routeName: &#39;The route is application...&#39; &#125;</code>，执行完回调之后继续转到子路由执行直到最后一个路由执行完毕，所有的路由执行完毕之后开始渲染页面。</li>
<li>页面的渲染则是从最后一个路由对应的模板开始，并沿着最近的父模板往回渲染。<br>为了验证是否是这样的执行顺序，下面修改<code>detail.js</code>和<br><code>comments.js</code>。在代码中加入一个模拟休眠的操作。<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/routes/posts/detail.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Route.extend(&#123;</span><br><span class="line"></span><br><span class="line">    model: <span class="function"><span class="keyword">function</span>(<span class="params">params</span>) </span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;params id = &#x27;</span> + params.post_id);</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;running in detail....&#x27;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//  执行一个循环，模拟休眠</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">10000000000</span>; i++) &#123;</span><br><span class="line">            </span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;The comment route executed...&#x27;</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> &#123; <span class="attr">id</span>: <span class="number">1</span>, <span class="attr">routeName</span>: <span class="string">&#x27;The route is detail..&#x27;</span> &#125;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);    </span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/routes/posts/detail/comments.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Route.extend(&#123;</span><br><span class="line">    model: <span class="function"><span class="keyword">function</span>(<span class="params">params</span>) </span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;params id = &#x27;</span> + params.post_id);</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;running in comment...&#x27;</span>); </span><br><span class="line">        <span class="comment">//  执行一个循环，模拟休眠</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">10000000000</span>; i++) &#123;</span><br><span class="line">            </span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> &#123; <span class="attr">id</span>: <span class="number">1</span>, <span class="attr">routeName</span>: <span class="string">&#x27;The route is comment...&#x27;</span>&#125;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
刷新页面，注意查看控制台输出信息和页面显示的内容。<br>新开一个窗口，执行URL：<a target="_blank" rel="noopener" href="http://localhost:4200/posts/2/comments">http://localhost:4200/posts/2/comments</a>。</li>
</ol>
<p><img src="/content/images/2016/03/30.png" alt="run result"></p>
<p>控制台输出到这里时处理等待（执行<code>for</code>循环），此时已经执行了两个路由<code>application</code>和<code>posts</code>，并且正在执行<code>detail</code>，但是页面是空白的，没有任何HTML元素。</p>
<p><img src="/content/images/2016/03/31.png" alt="run result"></p>
<p>在<code>detail</code>路由执行完成之后转到路由<code>comments</code>。然后执行到<code>for</code>循环模拟休眠，此时页面仍然是没有任何HTML元素。然后等到所有<code>route</code>执行完毕之后，界面才显示<code>model</code>回调里设置的信息。</p>
<p><img src="/content/images/2016/03/33.png" alt="run result"></p>
<p><img src="/content/images/2016/03/32.png" alt="run result"></p>
<p>每个子路由设置的信息都会渲染到最近一个父路由对应模板的<code>{{outlet}}</code>上面。</p>
<p><img src="/content/images/2016/03/34.png" alt="run result"></p>
<ol>
<li>渲染<code>comment</code><br>得到的内如为：“<code>comment</code>渲染完成”</li>
<li>渲染<code>comment</code>最近的父模板<code>comments</code><br>得到的内容为：“<code>comment</code>渲染完成 <code>comments</code>渲染完成”</li>
<li>渲染<code>comments</code>最近的父模板<code>detail</code><br>得到的内容为：“<code>comment</code>渲染完成 <code>comments</code>渲染完成 <code>detail</code>渲染完成”</li>
<li>渲染<code>detail</code>最近的父模板<code>posts</code><br>得到的内容为：“<code>comment</code>渲染完成 <code>comments</code>渲染完成 <code>detail</code>渲染完成 <code>posts</code>渲染完成”</li>
<li>渲染<code>posts</code>最近的父模板<code>application</code><br>得到的内容为：“<code>comment</code>渲染完成 <code>comments</code>渲染完成 <code>detail</code>渲染完成 <code>posts</code>渲染完成 <code>application</code>渲染完成”</li>
</ol>
<p>只要记住一句话：<strong>子模板的都会渲染到父模板的<code>{{outlet}}</code>上，最终所有的模板都会被渲染到<code>application</code>的<code>{{outlet}}</code>上。</strong></p>
<br>
博文完整代码放在[Github](https://github.com/ubuntuvim/my_emberjs_code)（博文经过多次修改，博文上的代码与github代码可能又出入，不过影响不大！），如果你觉得博文对你有点用，请在github项目上给我点个`star`吧。您的肯定对我来说是最大的动力！！

    </div>

      
    <div class="post-nav">
      <div class="post-nav-item post-nav-next">
        
          <span>〈 </span>
          <a href="/2020/01/18/ember-teach/templetes/%E7%AC%AC%E4%BA%8C%E7%AB%A0%E6%A8%A1%E6%9D%BF%E5%B0%8F%E7%BB%93/" rel="next" title="第二章模板小结">
          第二章模板小结
          </a>
        
      </div>
  
      <div class="post-nav-item post-nav-prev">
          
          <a href="/2020/01/18/ember-teach/templetes/%E5%B7%A5%E5%85%B7%E7%B1%BB%E7%9A%84%E5%8A%A9%E6%89%8B/" rel="prev" title="工具类的助手">
            工具类的助手
          </a>
          <span>〉</span>
        
      </div>
    </div>
  
  </article>
  <div class="toc-container">
    

  </div>
</div>


<div class="copyright">
    <span>本作品采用</span>
    <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by/4.0/">知识共享署名 4.0 国际许可协议</a>
    <span>进行许可。 转载时请注明原文链接。</span>
</div>
<div class="share">

</div>
<div class="post-container">
    <article class="post">
      <div id="container"></div>
    </article>
</div>

<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">
<script src="https://imsun.github.io/gitment/dist/gitment.browser.js"></script>
<script>
var gitment = new Gitment({
  id: '番外篇，路由、模板的执行、渲染顺序', // 可选。默认为 location.href
  owner: 'ubuntuvim',
  repo: 'ubuntuvim.github.io',
  oauth: {
    client_id: '48b3df0bf4ba1743e0a3',
    client_secret: 'c6c2cec09acb5eecc9d325d56cef187264aa1e16',
  },
})
gitment.render('container')
</script>



    </div>

    

  </div>
  <footer class="footer text-center">
    <div id="bottom-inner">
        <a class="bottom-item" href="http://xcoding.tech/">首页</a> |
        <a class="bottom-item" href="http://xcoding.tech/" target="_blank">主站</a> |
        <a class="bottom-item" href="https://github.com/ubuntuvim" target="_blank">GitHub</a> |
        <a class="bottom-item" href="https://hexo.io" target="_blank">Powered by hexo</a> |
        <a class="bottom-item" href="https://github.com/KevinOfNeu/hexo-theme-xoxo" target="_blank">Theme xoxo</a>
    </div>
</footer>


  <script src='https://unpkg.com/mermaid@7.1.2/dist/mermaid.min.js'></script>
  <script>
    if (window.mermaid) {
      mermaid.initialize({theme: 'forest'});
    }
  </script>


  
  <!-- scripts list from theme config.yml -->
  
    <script src="/js/zepto.min.js"></script>
  


<script>
  (function(window, document, undefined) {

    var timer = null;

    function returnTop() {
      cancelAnimationFrame(timer);
      timer = requestAnimationFrame(function fn() {
        var oTop = document.body.scrollTop || document.documentElement.scrollTop;
        if (oTop > 0) {
          document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
          timer = requestAnimationFrame(fn);
        } else {
          cancelAnimationFrame(timer);
        }
      });
    }

    var hearts = [];
    window.requestAnimationFrame = (function() {
      return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function(callback) {
          setTimeout(callback, 1000 / 60);
        }
    })();
    init();

    function init() {
      css(".heart{z-index:9999;width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: absolute;}.heart:after{top: -5px;}.heart:before{left: -5px;}");
      attachEvent();
      gameloop();
      addMenuEvent();
    }

    function gameloop() {
      for (var i = 0; i < hearts.length; i++) {
        if (hearts[i].alpha <= 0) {
          document.body.removeChild(hearts[i].el);
          hearts.splice(i, 1);
          continue;
        }
        hearts[i].y--;
        hearts[i].scale += 0.004;
        hearts[i].alpha -= 0.013;
        hearts[i].el.style.cssText = "left:" + hearts[i].x + "px;top:" + hearts[i].y + "px;opacity:" + hearts[i].alpha + ";transform:scale(" + hearts[i].scale + "," + hearts[i].scale + ") rotate(45deg);background:" + hearts[i].color;
      }
      requestAnimationFrame(gameloop);
    }

    /**
     * 给logo设置点击事件
     * 
     * - 回到顶部
     * - 出现爱心
     */
    function attachEvent() {
      var old = typeof window.onclick === "function" && window.onclick;
      var logo = document.getElementById("logo");
      if (logo) {
        logo.onclick = function(event) {
          returnTop();
          old && old();
          createHeart(event);
        }
      }
      
    }

    function createHeart(event) {
      var d = document.createElement("div");
      d.className = "heart";
      hearts.push({
        el: d,
        x: event.clientX - 5,
        y: event.clientY - 5,
        scale: 1,
        alpha: 1,
        color: randomColor()
      });
      document.body.appendChild(d);
    }

    function css(css) {
      var style = document.createElement("style");
      style.type = "text/css";
      try {
        style.appendChild(document.createTextNode(css));
      } catch (ex) {
        style.styleSheet.cssText = css;
      }
      document.getElementsByTagName('head')[0].appendChild(style);
    }

    function randomColor() {
      // return "rgb(" + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + ")";
      return "#F44336";
    }

    function addMenuEvent() {
      var menu = document.getElementById('menu-main-post');
      if (menu) {
        var toc = document.getElementById('toc');
        if (toc) {
          menu.onclick = function() {
            if (toc) {
              if (toc.style.display == 'block') {
                toc.style.display = 'none';
              } else {
                toc.style.display = 'block';
              }
            }
          };
        } else {
          menu.style.display = 'none';
        }
      }
    }

  })(window, document);
</script>

  



</body>
</html>
